//
// Created by 文道江 on 15/11/20.
// Copyright (c) 2015 文道江. All rights reserved.
//

#import <MediaPlayer/MediaPlayer.h>
#import "HybridCoreUIAction.h"
#import "HybridBridge.h"

@interface HybridCoreUIAction () <UIAlertViewDelegate>

@property(strong, nonatomic) MBProgressHUD *hud;
@property(assign, nonatomic) BOOL isSubscribe;

@end

@implementation HybridCoreUIAction

- (void)showToast:(id)params {
    NSTimeInterval duration = [params[@"duration"] floatValue];
    NSTimeInterval delay = [params[@"delay"] floatValue];

    if (!duration || duration == 0.0f) {
        duration = 1.5f;
    }
    if (!delay) {
        delay = 0.0f;
    }

    if (params[@"radius"] != nil) {
        CGFloat radius = [params[@"radius"] floatValue];
        [KSToastView ks_setAppearanceCornerRadius:radius];
    }

    AppTheme *appTheme = [HybridBridge defaultHybridBridge].theme;

    if (params[@"bgColor"] != nil) {
        NSString *bgColor = params[@"bgColor"];
        if ([bgColor hasPrefix:@"#"]) {
            [KSToastView ks_setAppearanceBackgroundColor:[[MAThemeKit colorWithHexString:[bgColor substringFromIndex:1]] colorWithAlphaComponent:0.8]];
        }
    } else if (appTheme != nil) {
        [KSToastView ks_setAppearanceBackgroundColor:[appTheme.primaryColor colorWithAlphaComponent:0.8]];
    }

    if (params[@"color"] != nil) {
        NSString *color = params[@"color"];
        if ([color hasPrefix:@"#"]) {
            NSLog(@"color %@", [color substringFromIndex:1]);
            [KSToastView ks_setAppearanceTextColor:[MAThemeKit colorWithHexString:[color substringFromIndex:1]]];
        }
    } else if (appTheme != nil) {
        [KSToastView ks_setAppearanceTextColor:appTheme.color];
    }

    if (params[@"marginBottom"] != nil) {
        [KSToastView ks_setAppearanceOffsetBottom:[params[@"marginBottom"] floatValue]];
    }

    if (params[@"maxWidth"] != nil) {
        [KSToastView ks_setAppearanceMaxWidth:[params[@"maxWidth"] floatValue]];
    }

    if (params[@"maxLine"] != nil) {
        [KSToastView ks_setAppearanceMaxLines:[params[@"maxLine"] integerValue]];
    }

    [KSToastView ks_showToast:params[@"message"] duration:duration delay:delay completion:^{
        [self callback:YES data:nil];
    }];
}

- (void)showIndicator:(id)params {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    if (params[@"text"]) {
        hud.labelText = params[@"text"];
    }
    if (params[@"mode"]) {
        hud.mode = (MBProgressHUDMode) [params[@"mode"] integerValue];
    }
}

- (void)hideIndicator:(id)params {
    [MBProgressHUD hideHUDForView:self.webView animated:YES];
}

/**
 * 显示提示弹窗
 */
- (void)showAlert:(id)params {
    NSString *title = params[@"title"] == nil ? @"提示" : params[@"title"];
    NSString *message = params[@"message"] == nil ? @"" : params[@"message"];
    NSString *cancelText = params[@"cancelText"] == nil ? @"取消" : params[@"cancelText"];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message delegate:self
                                              cancelButtonTitle:cancelText otherButtonTitles:nil];
    alertView.tag = 67185;
    [alertView show];
}

/**
 * 显示确认对话框
 */
- (void)showConfirm:(id)params {
    NSString *title = params[@"title"] == nil ? @"提示" : params[@"title"];
    NSString *message = params[@"message"] == nil ? @"" : params[@"message"];
    NSString *cancelText = params[@"cancelText"] == nil ? @"取消" : params[@"cancelText"];
    NSString *okText = params[@"okText"] == nil ? @"确认" : params[@"okText"];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message delegate:self
                                              cancelButtonTitle:cancelText otherButtonTitles:okText, nil];
    alertView.tag = 67186;
    [alertView show];
}

/**
 * 显示本地通知
 */
- (void)showNotify:(id)params {
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification == nil) {
        [self callback:NO data:@"create localNotification object fail."];
        return;
    }
    NSInteger delay = params[@"delay"] !=nil ? [params[@"delay"] integerValue] : 0;
    //设置本地通知的触发时间（如果要立即触发，无需设置），这里设置为20妙后
    if (delay > 0){
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow: delay];
        //设置本地通知的时区
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
    }
    //设置通知的内容
    localNotification.alertBody = params[@"title"] == nil ? @"提示" : params[@"title"];
    //设置通知动作按钮的标题
    localNotification.alertAction = params[@"message"] == nil ? @"查看" : params[@"message"];
    //设置提醒的声音，可以自己添加声音文件，这里设置为默认提示声
    localNotification.soundName = params[@"sound"] == nil ? UILocalNotificationDefaultSoundName : params[@"sound"];

    if (params[@"iconBadge"] != nil) {
        localNotification.applicationIconBadgeNumber = [params[@"iconBadge"] integerValue];
    }

    if (params[@"data"] != nil){
        //设置通知的相关信息，这个很重要，可以添加一些标记性内容，方便以后区分和获取通知的信息
        localNotification.userInfo = params[@"data"];
    }

    if (delay > 0){
        //在规定的日期触发通知
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }else{
        //立即触发一个通知
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }

    [self callback:YES data:@"success"];
}

#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (alertView.tag) {
        case 67185:
            if (buttonIndex == 0) {
                [self callback:YES data:@"close"];
            }
            break;

        case 67186:
            if (buttonIndex == 0) {
                [self callback:NO data:@"cancel"];
            } else {
                [self callback:YES data:@"ok"];
            }
            break;
        default:
            break;
    }
}


@end